package Handler;

import dao.*;
import dao.impl.*;
import pojo.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

public class SuperUserHandler {

    public void showUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            IUserDao userDao = new UserDaoImpl();
            List<User> userList = userDao.findAll();
            request.setAttribute("userList", userList);
            request.getRequestDispatcher("/admin/SuperUserIndex.jsp").forward(request, response);

        } catch (Exception e) {
            e.printStackTrace();
            request.getRequestDispatcher("/user/login.jsp").forward(request, response);
        }
    }
    public void showProduct(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            IProductDao productDao = new ProductDaoImpl();
            List<Product> productList = productDao.findAll();
            request.setAttribute("productList", productList);
            request.getRequestDispatcher("/admin/SuperUserProduct.jsp").forward(request, response);

        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("message", "显示错误");
            request.getRequestDispatcher("/shop/error.jsp").forward(request, response);
        }
    }

    public void showSupplier(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            ISupplierDao supplierDao=new SupplierDaoImpl();
            List<Supplier> suppliers=supplierDao.findAll();
            request.setAttribute("suppliers",suppliers);
            request.getRequestDispatcher("/admin/supplierList.jsp").forward(request,response);
        }catch (Exception e){
            e.printStackTrace();
            request.setAttribute("message", "显示错误");
            request.getRequestDispatcher("/shop/error.jsp").forward(request, response);
        }
    }

    public void addSupplier(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {

            request.getRequestDispatcher("/admin/addsupplier.jsp").forward(request,response);
        }catch (Exception e){
            e.printStackTrace();
            request.setAttribute("message", "添加错误");
            request.getRequestDispatcher("/shop/error.jsp").forward(request, response);
        }
    }


    public void showSupplierProduct(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            int id= Integer.parseInt(request.getParameter("supplierid"));
            ISupplierDao supplierDao=new SupplierDaoImpl();
            Supplier supplier = supplierDao.findByid(id);
            request.setAttribute("Supplier", supplier);
            request.getRequestDispatcher("/admin/SupplierProduct.jsp").forward(request, response);

        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("message", "显示错误");
            request.getRequestDispatcher("/shop/error.jsp").forward(request, response);
        }
    }

    public void deleteSupplier(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            int id= Integer.parseInt(request.getParameter("supplierid"));
            ISupplierDao supplierDao=new SupplierDaoImpl();
            supplierDao.deleteSupplier(id);
            showSupplier(request,response);
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("message", "显示错误");
            request.getRequestDispatcher("/shop/error.jsp").forward(request, response);
        }
    }

    public void addSuppliers(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            String name=request.getParameter("name");
            ISupplierDao supplierDao=new SupplierDaoImpl();
            supplierDao.addSupplier(name);
            showSupplier(request,response);
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("message", "显示错误");
            request.getRequestDispatcher("/shop/error.jsp").forward(request, response);
        }
    }

    public void showOrder(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            IMyOrderDao myOrderDao=new MyOrderDaoImpl();
            List<MyOrder> myOrderList=myOrderDao.findAll();
            request.setAttribute("myOrderList",myOrderList);
            request.getRequestDispatcher("/admin/SuperUserOrder.jsp").forward(request, response);

        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("message", "显示错误");
            request.getRequestDispatcher("/shop/error.jsp").forward(request, response);
        }
    }

    public void showItems(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            int id= Integer.parseInt(request.getParameter("orderid"));
            IMyOrderDao myOrderDao=new MyOrderDaoImpl();
            MyOrder myOrder=myOrderDao.findById(id);
            request.setAttribute("order",myOrder);
            request.getRequestDispatcher("/admin/SuperUserItems.jsp").forward(request, response);

        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("message", "显示错误");
            request.getRequestDispatcher("/shop/error.jsp").forward(request, response);
        }
    }

    public void updateStatus(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            int id= Integer.parseInt(request.getParameter("orderid"));
            IMyOrderDao myOrderDao=new MyOrderDaoImpl();
            MyOrder myOrder=myOrderDao.findById(id);
            request.setAttribute("order",myOrder);
            request.getRequestDispatcher("/admin/updateStatus.jsp").forward(request, response);
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("message", "显示错误");
            request.getRequestDispatcher("/shop/error.jsp").forward(request, response);
        }
    }

    public void updateOrder(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            int id= Integer.parseInt(request.getParameter("orderid"));
            IMyOrderDao myOrderDao=new MyOrderDaoImpl();
            MyOrder myOrder=myOrderDao.findById(id);
            int status_id=Integer.parseInt(request.getParameter("status"));
            IOrderStatusDao statausDao=new OrderStatusDaoImpl();
            OrderStatus status=statausDao.findOrderStatusById(status_id);
            myOrder.setOrderStatus(status);
            myOrderDao.updateMyOrder(myOrder);
            showOrder(request,response);
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("message", "显示错误");
            request.getRequestDispatcher("/shop/error.jsp").forward(request, response);
        }
    }
}
